package miiteen.demo.duty.controller;

import miiteen.common.json.JsonUtil;
import miiteen.core.configuration.MeetingProperties;
import miiteen.core.result.ResultMessage;
import miiteen.core.result.ResultMessageTool;
import miiteen.customsportal.basic.service.BasicBrowsingHistoryService;
import miiteen.customsportal.basic.beans.BasicBrowsingHistory;
import miiteen.demo.HB2012Service.Service;
import miiteen.demo.HB2012Service.XmlInfo;
import miiteen.demo.duty.beans.Instruction;
import miiteen.demo.duty.beans.Leader;
import miiteen.demo.duty.service.InstructionService;
import miiteen.demo.utils.CommonUtil;
import miiteen.sys.base.controller.BaseAction;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping(value = "/demo/duty/instruction")
@CrossOrigin
public class InstructionController extends BaseAction {

    @Autowired
    private InstructionService instructionService;

    @Autowired
    private Service HB2012Service;

    @Autowired
    private CommonUtil commonUtil;

    @Autowired
    private BasicBrowsingHistoryService basicBrowsingHistoryService;

    @RequestMapping("/getInsByUserName")
    @ResponseBody
    public ResultMessage<Object> getInstructionInfo(@RequestParam String start, @RequestParam String count, @RequestParam String userName) {
        List<Instruction> list = new ArrayList<>();
        String leaderStr = MeetingProperties.getConfigByKey("duty.leaders");
        if (leaderStr.contains(userName) && !"".equals(userName)) {
            String[] leaders = leaderStr.split(",");
            for (int i = 0; i < leaders.length; i++) {
                if (leaders[i].contains(userName)) {
                    userName = leaders[i].substring(leaders[i].indexOf(" ") + 1);
                }
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("start", Integer.parseInt(start));
        map.put("count", Integer.parseInt(count));
        map.put("userName", userName);
        commonUtil.handleLikeSearch(map, "userName");
        list = instructionService.findByUserName(map);
        Map<String, Object> limitMap = new HashMap<>();


        for (Instruction instruction : list
        ) {

            instruction.setUrl(MeetingProperties.getConfigByKey("duty.URL") + "?id=" + instruction.getId());
            limitMap.clear();
            limitMap.put("id", instruction.getId());
            limitMap.put("ifMod", 0);
            BasicBrowsingHistory byIdNumber = basicBrowsingHistoryService.getByIdNumber(limitMap);
            instruction.setTodayNumber(byIdNumber == null ? 0 : byIdNumber.getDayNumber());
            instruction.setTotalNumber(byIdNumber == null ? 0 : byIdNumber.getAllNumber());
        }
        if (list.size() > 0) {
            return ResultMessageTool.Success("查询成功", list);
        } else {
            return ResultMessageTool.Error("查询失败");
        }
    }

    @RequestMapping("/searchAllIns")
    @ResponseBody
    public ResultMessage<Object> searchAllInstructions(@RequestParam String start, @RequestParam String count, @RequestParam String keyWords) {
        Map<String, Object> map = new HashMap<>();
        int startNum = Integer.parseInt(start);
        if (StringUtils.isNotBlank(keyWords)) {
            map.put("keyWords", "%" + keyWords + "%");
        }
        Instruction number = instructionService.findTotalCount(map);
        map.put("start", startNum > 1 ? 1 + 10 * (startNum - 1) : startNum);
        map.put("count", Integer.parseInt(count));
        commonUtil.handleLikeSearch(map, "keyWords");
        List<Instruction> list = instructionService.searchAllInstructions(map);
        Map<String, Object> limitMap = new HashMap<>();
        for (Instruction instruction : list
        ) {
            instruction.setUrl(MeetingProperties.getConfigByKey("duty.URL") + "?id=" + instruction.getId());
            limitMap.clear();
            limitMap.put("id", instruction.getId());
            limitMap.put("ifMod", 0);
            BasicBrowsingHistory byIdNumber = basicBrowsingHistoryService.getByIdNumber(limitMap);
            instruction.setTodayNumber(byIdNumber == null ? 0 : byIdNumber.getDayNumber());
            instruction.setTotalNumber(byIdNumber == null ? 0 : byIdNumber.getAllNumber());
        }

        map.clear();
        //map.put("infoList",list);
        //map.put("totalCount",Integer.parseInt(number.getRowNum()));
        if (list.size() > 0) {
            return ResultMessageTool.Success(number.getRowNum(), list);
        } else {
            return ResultMessageTool.Error("0", new ArrayList<>());
        }
    }


    @RequestMapping("/getLeaderList")
    @ResponseBody
    public ResultMessage<Object> getLeaderList() {
        List<Leader> list = new ArrayList<>();
        // Map<String,Object> map=new LinkedHashMap<>();//需要按顺序存放
        String leaderStr = MeetingProperties.getConfigByKey("duty.leaders");
        String[] leaders = leaderStr.split(",");
        for (int i = 0; i < leaders.length; i++) {
            String[] leader = leaders[i].split(" ");
            Leader leader1 = new Leader();
            leader1.setId(leader[0]);
            leader1.setName(leader[1]);
            list.add(leader1);

        }

        return ResultMessageTool.Success("查询成功", list);
    }


    @RequestMapping("/getCommFileList")
    @ResponseBody
    public ResultMessage<Object> getCommonFileList(@RequestParam String start, @RequestParam String count, @RequestParam String title, @RequestParam String name) {
        Map<String, Object> map = new HashMap<>();
        try {
            map = HB2012Service.getCommonFileList(start, count, title, name);
        } catch (Exception e) {
            return ResultMessageTool.Error("查询出错");
        }
        if (map.get("resourceList") != null) {
            return ResultMessageTool.Success("查询成功", map);
        } else {
            return ResultMessageTool.Error("查询失败");
        }
    }


}
